package com.atguigu.gmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.server.WebFilter;

import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.Lock;

@Configuration
public class CorsConfig {

    //配置配置类
    @Bean
    public WebFilter webFilter(){
        //创建对象
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //设置是否允许跨类
        corsConfiguration.setAllowCredentials(true); //允许携带cookie
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedHeader("*");

        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(urlBasedCorsConfigurationSource);
    }
}
